<?php
/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of MySQL
 *
 * @author gurzaf
 */
class MySQL {
    private $_host;
    private $_user;
    private $_pass;
    private $_link;
    private $_message;
    private $_errNum;
    private $_result;
    private $_db;

    function __construct($_host, $_user, $_pass, $_db) {
        $this->_host = $_host;
        $this->_user = $_user;
        $this->_pass = $_pass;
        $this->_db = $_db;
    }

    public function get_host() {
        return $this->_host;
    }

    public function set_host($_host) {
        $this->_host = $_host;
    }

    public function get_user() {
        return $this->_user;
    }

    public function set_user($_user) {
        $this->_user = $_user;
    }

    public function get_pass() {
        return $this->_pass;
    }

    public function set_pass($_pass) {
        $this->_pass = $_pass;
    }

    public function get_link() {
        return $this->_link;
    }

    public function set_link($_link) {
        $this->_link = $_link;
    }

    public function get_message() {
        return $this->_message;
    }

    public function set_message($_message) {
        $this->_message = $_message;
    }

    public function get_errNum() {
        return $this->_errNum;
    }

    public function set_errNum($_errNum) {
        $this->_errNum = $_errNum;
    }

    public function get_result() {
        return $this->_result;
    }

    public function set_result($_result) {
        $this->_result = $_result;
    }

    public function get_db() {
        return $this->_db;
    }

    public function set_db($_db) {
        $this->_db = $_db;
    }

    
    public function conectarse(){
        $this->_link = mysql_connect($this->_host, $this->_user, $this->_pass);
        if(!$this->_link){
            $this->_errNum = mysql_errno();
            $this->_message = mysql_error();
            return false;
        }else{
            if(!@mysql_select_db($this->_db)){
                $this->_errNum = mysql_errno();
                $this->_message = mysql_error();
                return false;
            }else{
                return true;
            }
        }
    }

    public function insertar($query){
        if($query == ""){
            $this->_message = "No se ha indicado la consulta";
            return 0;
        }else{
            $this->_result = @mysql_query($query,  $this->_link);
            if(!$this->_result){
                $this->_errNum = mysql_errno();
                $this->_message = mysql_error();
                return 0;
            }else{
                return $this->_result;
            }
        }
    }

    public function consultar($query){
        if(!$this->insertar($query)){
            return 0;
        }else{
            $matriz="";
            $j=0;
            while ($row = mysql_fetch_row($this->_result))
            {
                for ($i = 0; $i < mysql_num_rows($this->_result); $i++)
                {
                    $matriz[$j][$i]=$row[$i];
                }
                $j++;
            }
            return $matriz;
        }
    }

    public function desconectarse(){
        if(!@mysql_close($this->_link)){
            $this->_message = mysql_error();
            $this->_errNum = mysql_errno();
            return 0;
        }else{
            return 1;
        }
    }
}
